<template>
  <el-dialog
    width="50%"
    :title="!dataForm.deptId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form size="mini" :model="dataForm" :rules="dataRule" ref="dataForm" label-width="150px">
    <el-row type="flex" class="row-bg">
      <el-col :span="8">
        <el-form-item label="机构代码" prop="deptCode">
          <el-input clearable v-model="dataForm.deptCode" placeholder="机构代码"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="16">
        <el-form-item label="机构名称" prop="deptName">
          <el-input clearable v-model="dataForm.deptName" placeholder="机构名称"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row type="flex" class="row-bg">
      <el-col :span="8">
        <el-form-item label="机构类型" prop="deptType">
          <commonSelect :typeCode="'DEPT_TYPE'" v-model="dataForm.deptType"></commonSelect>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="上级机构" prop="parentDeptId">
          <commonSelect :typeCode="'DEPT_NAME'" v-model="dataForm.parentDeptId"></commonSelect>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="排序" prop="orderNum">
          <el-input-number clearable v-model="dataForm.orderNum" :min="1" placeholder="顺序" style="width: 100%"></el-input-number>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row type="flex" class="row-bg">
      <el-col :span="16">
        <el-form-item label="单位地址" prop="deptAddress">
          <el-input clearable v-model="dataForm.deptAddress" placeholder="单位地址"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row type="flex" class="row-bg">
      <el-col :span="16">
        <el-form-item label="备注" prop="remark">
          <el-input clearable v-model="dataForm.remark" placeholder="备注" type="textarea" :autosize="{ minRows: 3, maxRows: 5}"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="visible = false">取消</el-button>
      <el-button size="mini" type="primary" @click="dataFormSubmit()" :loading="btnLoading">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import commonSelect from '@/components/common-select'
  export default {
    components: { commonSelect },
    data () {
      return {
        visible: false,
        btnLoading: false,
        dataForm: {
          deptId: '',
          deptName: '',
          deptCode: '',
          parentDeptId: '',
          deptType: '',
          deptAddress: '',
          deptPostCode: '',
          accountPrefix: '',
          enterpriseType: '',
          enterpriseSubType: '',
          businessType: '',
          isApprovalPerm: '',
          isPiPerm: '',
          isPassKeeping: '',
          isResponsibleDept: '',
          passReturnDays: '',
          isReport: '',
          manageOffice: '',
          passportFreezeState: '',
          passportFreezeDate: '',
          passFreezeDesc: '',
          orderNum: '',
          remark: ''
        },
        dataRule: {
          deptName: [
            { required: true, message: '单位名称不能为空', trigger: 'blur' }
          ],
          deptType: [
            { required: true, message: '供应商类型不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    mounted () {
    },
    methods: {
      init (id) {
        this.dataForm.deptId = id || ''
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.deptId) {
            this.$http({
              url: this.$http.requestUrl(`/sys/dept/info/${this.dataForm.deptId}`),
              method: 'get',
              params: this.$http.requestParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.deptId = data.info.deptId
                this.dataForm.deptName = data.info.deptName
                this.dataForm.deptCode = data.info.deptCode
                this.dataForm.parentDeptId = data.info.parentDeptId
                this.dataForm.deptType = data.info.deptType
                this.dataForm.deptAddress = data.info.deptAddress
                this.dataForm.deptPostCode = data.info.deptPostCode
                this.dataForm.accountPrefix = data.info.accountPrefix
                this.dataForm.enterpriseType = data.info.enterpriseType
                this.dataForm.enterpriseSubType = data.info.enterpriseSubType
                this.dataForm.businessType = data.info.businessType
                this.dataForm.isApprovalPerm = data.info.isApprovalPerm
                this.dataForm.isPiPerm = data.info.isPiPerm
                this.dataForm.isPassKeeping = data.info.isPassKeeping
                this.dataForm.isResponsibleDept = data.info.isResponsibleDept
                this.dataForm.passReturnDays = data.info.passReturnDays
                this.dataForm.isReport = data.info.isReport
                this.dataForm.manageOffice = data.info.manageOffice
                this.dataForm.passportFreezeState = data.info.passportFreezeState
                this.dataForm.passportFreezeDate = data.info.passportFreezeDate
                this.dataForm.passFreezeDesc = data.info.passFreezeDesc
                this.dataForm.orderNum = data.info.orderNum
                this.dataForm.remark = data.info.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.btnLoading = true
            this.$http({
              url: this.$http.requestUrl(`/sys/dept/${!this.dataForm.deptId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.requestData({
                'deptId': this.dataForm.deptId || undefined,
                'deptName': this.dataForm.deptName,
                'deptCode': this.dataForm.deptCode,
                'parentDeptId': this.dataForm.parentDeptId,
                'deptType': this.dataForm.deptType,
                'deptAddress': this.dataForm.deptAddress,
                'deptPostCode': this.dataForm.deptPostCode,
                'accountPrefix': this.dataForm.accountPrefix,
                '*enterpriseType': this.dataForm.enterpriseType,
                '*enterpriseSubType': this.dataForm.enterpriseSubType,
                '*businessType': this.dataForm.businessType,
                'isApprovalPerm': this.dataForm.isApprovalPerm,
                'isPiPerm': this.dataForm.isPiPerm,
                'isPassKeeping': this.dataForm.isPassKeeping,
                'isResponsibleDept': this.dataForm.isResponsibleDept,
                'passReturnDays': this.dataForm.passReturnDays,
                'isReport': this.dataForm.isReport,
                'manageOffice': this.dataForm.manageOffice,
                'passportFreezeState': this.dataForm.passportFreezeState,
                'passportFreezeDate': this.dataForm.passportFreezeDate,
                'passFreezeDesc': this.dataForm.passFreezeDesc,
                'orderNum': this.dataForm.orderNum,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
              this.btnLoading = false
            })
          }
        })
      }
    }
  }
</script>
